/*
 * Copyright (c) 2009 The University of Edinburgh
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors: Lei Zhang
 * NOTE: This file is not fully tested and bugs may exist.
 */

#include "arch/unicore/insts/mem.hh"
#include "base/loader/symtab.hh"

using namespace UnicoreISA;

std::string
MemOp::generateDisassembly(Addr pc, const SymbolTable *symtab) const
{
    std::stringstream ss;
    std::string myMnemonic(mnemonic);

    // write back, post-sub
    if (myMnemonic.rfind("psw") == myMnemonic.length() - 3) { 
        myMnemonic.replace(myMnemonic.length() - 3, 3, ".w");

        // remove the appending 'x'
        myMnemonic = myMnemonic.substr(0, myMnemonic.length() - 1);
        ccprintf(ss, "%-10s ", myMnemonic);
       
        if (flags[IsLoad]) 
            printReg(ss, _destRegIdx[0]);
        else
            printReg(ss, _srcRegIdx[2]);
        ss << ", ";

        ss << "[";
        printReg(ss, _srcRegIdx[0]);
        ss << "]-, ";
        printReg(ss, _srcRegIdx[1]);
    }
    // write back, post-add
    else if (myMnemonic.rfind("pw") == myMnemonic.length() - 2) { 
        myMnemonic.replace(myMnemonic.length() - 2, 2, ".w");

        // remove the appending 'x'
        myMnemonic = myMnemonic.substr(0, myMnemonic.length() - 1);
        ccprintf(ss, "%-10s ", myMnemonic);

        if (flags[IsLoad]) 
            printReg(ss, _destRegIdx[0]);
        else
            printReg(ss, _srcRegIdx[2]);
        ss << ", ";

        ss << "[";
        printReg(ss, _srcRegIdx[0]);
        ss << "]+, ";
        printReg(ss, _srcRegIdx[1]);
    }
    // write back, pre-sub
    else if (myMnemonic.rfind("sw") == myMnemonic.length() - 2) { 
        myMnemonic.replace(myMnemonic.length() - 2, 2, ".w");

        // remove the appending 'x'
        myMnemonic = myMnemonic.substr(0, myMnemonic.length() - 1);
        ccprintf(ss, "%-10s ", myMnemonic);

        if (flags[IsLoad]) 
            printReg(ss, _destRegIdx[0]);
        else
            printReg(ss, _srcRegIdx[2]);
        ss << ", ";

        ss << "[";
        printReg(ss, _srcRegIdx[0]);
        ss << "-], ";
        printReg(ss, _srcRegIdx[1]);
    }
    // write back, pre-add
    else if (myMnemonic.rfind('w') == myMnemonic.length() - 1 &&
            myMnemonic.length() > 3) { // in case it's 'ldw'
        myMnemonic.replace(myMnemonic.length() - 1, 1, ".w");

        // remove the appending 'x'
        myMnemonic = myMnemonic.substr(0, myMnemonic.length() - 1);
        ccprintf(ss, "%-10s ", myMnemonic);

        if (flags[IsLoad]) 
            printReg(ss, _destRegIdx[0]);
        else
            printReg(ss, _srcRegIdx[2]);
        ss << ", ";
        
        ss << "[";
        printReg(ss, _srcRegIdx[0]);
        ss << "+], ";
        printReg(ss, _srcRegIdx[1]);
    }
    // pre-sub
    else if (myMnemonic.rfind('s') == myMnemonic.length() - 1) {
        myMnemonic = myMnemonic.substr(0, myMnemonic.length() - 1);

        // remove the appending 'x'
        myMnemonic = myMnemonic.substr(0, myMnemonic.length() - 1);
        ccprintf(ss, "%-10s ", myMnemonic);

        if (flags[IsLoad]) 
            printReg(ss, _destRegIdx[0]);
        else
            printReg(ss, _srcRegIdx[2]);
        ss << ", ";

        ss << "[";
        printReg(ss, _srcRegIdx[0]);
        ss << "-], ";
        printReg(ss, _srcRegIdx[1]);
    }
    // pre-add
    else {
        // remove the appending 'x'
        myMnemonic = myMnemonic.substr(0, myMnemonic.length() - 1);
        ccprintf(ss, "%-10s ", myMnemonic);

        if (flags[IsLoad]) 
            printReg(ss, _destRegIdx[0]);
        else
            printReg(ss, _srcRegIdx[2]);
        ss << ", ";

        ss << "[";
        printReg(ss, _srcRegIdx[0]);
        ss << "+], ";
        printReg(ss, _srcRegIdx[1]);
    }

    return ss.str();
}

std::string
MemDispOp::generateDisassembly(Addr pc, const SymbolTable *symtab) const
{
    std::stringstream ss;
    std::string myMnemonic(mnemonic);

    // write back, post-sub
    if (myMnemonic.rfind("psw") == myMnemonic.length() - 3) { 
        myMnemonic.replace(myMnemonic.length() - 3, 3, ".w");
        ccprintf(ss, "%-10s ", myMnemonic);
       
        if (flags[IsLoad]) 
            printReg(ss, _destRegIdx[0]);
        else
            printReg(ss, _srcRegIdx[1]);
        ss << ", ";

        ss << "[";
        printReg(ss, _srcRegIdx[0]);
        ss << "]-, #" << (int32_t)disp;
    }
    // write back, post-add
    else if (myMnemonic.rfind("pw") == myMnemonic.length() - 2) { 
        myMnemonic.replace(myMnemonic.length() - 2, 2, ".w");
        ccprintf(ss, "%-10s ", myMnemonic);

        if (flags[IsLoad]) 
            printReg(ss, _destRegIdx[0]);
        else
            printReg(ss, _srcRegIdx[1]);
        ss << ", ";

        ss << "[";
        printReg(ss, _srcRegIdx[0]);
        ss << "]+, #" << (int32_t)disp;
    }
    // write back, pre-sub
    else if (myMnemonic.rfind("sw") == myMnemonic.length() - 2) { 
        myMnemonic.replace(myMnemonic.length() - 2, 2, ".w");
        ccprintf(ss, "%-10s ", myMnemonic);

        if (flags[IsLoad]) 
            printReg(ss, _destRegIdx[0]);
        else
            printReg(ss, _srcRegIdx[1]);
        ss << ", ";

        ss << "[";
        printReg(ss, _srcRegIdx[0]);
        ss << "-], #" << (int32_t)disp;
    }
    // write back, pre-add
    else if (myMnemonic.rfind('w') == myMnemonic.length() - 1 &&
            myMnemonic.length() > 3) { // in case it's 'ldw'
        myMnemonic.replace(myMnemonic.length() - 1, 1, ".w");
        ccprintf(ss, "%-10s ", myMnemonic);

        if (flags[IsLoad]) 
            printReg(ss, _destRegIdx[0]);
        else
            printReg(ss, _srcRegIdx[1]);
        ss << ", ";
        
        ss << "[";
        printReg(ss, _srcRegIdx[0]);
        ss << "+], #" << (int32_t)disp;
    }
    // pre-sub
    else if (myMnemonic.rfind('s') == myMnemonic.length() - 1) {
        myMnemonic = myMnemonic.substr(0, myMnemonic.length() - 1);
        ccprintf(ss, "%-10s ", myMnemonic);

        if (flags[IsLoad]) 
            printReg(ss, _destRegIdx[0]);
        else
            printReg(ss, _srcRegIdx[1]);
        ss << ", ";

        ss << "[";
        printReg(ss, _srcRegIdx[0]);
        ss << "-], #" << (int32_t)disp;
    }
    // pre-add
    else {
        ccprintf(ss, "%-10s ", myMnemonic);

        if (flags[IsLoad]) 
            printReg(ss, _destRegIdx[0]);
        else
            printReg(ss, _srcRegIdx[1]);
        ss << ", ";

        ss << "[";
        printReg(ss, _srcRegIdx[0]);
        ss << "+], #" << (int32_t)disp;
    }

    return ss.str();
}
